//
//  StoreLogoViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/11/6.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "StoreLogoViewController.h"
#import "THWAlertControler.h"
#import "SelectOnePicture.h"
#import "StoreIdCardViewController.h"

@interface StoreLogoViewController ()
{
    NSData *_imaData;
}
@property (strong, nonatomic) SelectOnePicture *OnePicture;
@property (weak, nonatomic) IBOutlet UIButton *logoBtn;
@property (weak, nonatomic) IBOutlet UIButton *ClickBtn;

@end

@implementation StoreLogoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"店铺LOGO";
    
    _ClickBtn.layer.borderColor = [[UIColor redColor]CGColor];
    _ClickBtn.layer.borderWidth = 1.0f;
    _ClickBtn.layer.masksToBounds = YES;
}

- (IBAction)selectPic:(id)sender {
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
    {  // 没权限，提醒用户开启权限
        THWAlertControler *qq = [[THWAlertControler alloc]init];
        UIAlertController *alert = [qq alertmessage:@"请先允许店猫访问你的相册及相机功能，前往设置？" Title:nil andBlock:^{
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:url];
        }];
        [self presentViewController:alert animated:YES completion:nil];
        return;
    }
    // 调用相册
    if (!_OnePicture) {
        _OnePicture = [[SelectOnePicture alloc]initWithFrame:CGRectZero];
        _OnePicture.ratioOfWidthAndHeight = 200/200;
        _OnePicture.enableCutImg  =YES;
        _OnePicture.superController = self;
    }
    // 点击相册的回调
    
    [_OnePicture toSelectImage:^(NSData *imageData) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"post_disappear"  object:nil];
//        NSString *tempstr = API(MineShopIma);
//        [weakSelf imageUpload:imageData withUrl:tempstr];
        [_logoBtn setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
        _imaData = imageData;
    }];
}
- (IBAction)uploadClick:(id)sender {
    if (!_imaData) {
        [self.view Message:@"请上传图片"];
    }else{
        [self imageUpload:_imaData withUrl:API(MineShopIma)];
    }
}
- (void)imageUpload:(NSData*)data withUrl:(NSString *)url
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyyMMddHHmmss";
    NSString *str = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
    NSDictionary *dic = @{@"name":@"upload_file",@"type":@"image/png",@"tmp_name":fileName,@"size":@(data.length)};
    NSDictionary *para = @{@"flag":@"1",@"upload_file":@[dic], @"userToken":[Utils getUserToken]};
    [self.view loadingOnAnyView];
    [THWManager PostImageWithData:url imageData:data otherParamters:para success:^(NSURLSessionDataTask *task, id responseObject) {
        [self.view removeAnyView];
        
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithDictionary:_para];
        [dic setObject:responseObject[@"msg"] forKey:@"shopImg"];
        [self.view Message:@"上传成功"];
        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            StoreIdCardViewController *idCard = [[StoreIdCardViewController alloc] init];
            idCard.para = dic;
            [self.navigationController pushViewController:idCard animated:YES];
        });
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [self.view removeAnyView];
        [self.view Message:@"请重新上传"];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
